import { Context, Session } from 'koishi'
import { recordError, render } from "../../utils/util";
import { checkUserExist, createUser, getUserInfo, getUserInfoFormat, harvestSun, deleteAllUser, deleteAllPlant } from "../../database/userList";
import { deleteAllUpdateRecode } from "../../database/dataUpdate";
import { pathToFileURL } from 'url'
import { resolve } from 'path'


// 注意 指令不要重名
const AllAlias = {
    'me': 1,
    '我的': 1,
    'sun': 1,
    '收': 1,
}

const getImgUrl = (name: string) => {
    return pathToFileURL(resolve(__dirname, `../../state/img/${name}`)).href
}

const checkUser = async (ctx: Context, userId: string, messageData: any) => {
    const hasUser = await checkUserExist(ctx, userId)
    if (!hasUser) {
        const res = await createUser(ctx, messageData)
        if (!res) recordError(ctx, userId, '创建用户失败')
        else return res
    }
    else return hasUser
}

// 管理员
const admin = ['2509689194']
// 校验权限
const checkPermission = (messageData: any) => {
    return admin.includes(messageData.user.id)
}


const commandList = [
    {
        name: '删除所有记录',
        alias: ['删除所有记录'],
        fn: async (messageData: any, ctx: Context, session: Session) => {
            if (checkPermission(messageData)) {
                await deleteAllUpdateRecode(ctx)
                session.sendQueued('记录已清空')
            }
            else {
                session.sendQueued('权限不足')
            }
        }
    },
    {
        name: '删除所有用户',
        alias: ['删除所有用户'],
        fn: async (messageData: any, ctx: Context, session: Session) => {
            if (checkPermission(messageData)) {
                await deleteAllUser(ctx)
                session.sendQueued('用户已清空')
            }
            else {
                session.sendQueued('权限不足')
            }
        }
    },
    {
        name: '删除所有植物',
        alias: ['删除所有植物'],
        fn: async (messageData: any, ctx: Context, session: Session) => {
            if (checkPermission(messageData)) {
                const userId = messageData.user.id
                deleteAllPlant(ctx, userId)
                session.sendQueued('植物已清空')
            }
            else {
                session.sendQueued('权限不足')
            }
        }
    },
    {
        name: '用户信息',
        alias: ['me', '我的'], // 别名
        fn: async (messageData: any, ctx: Context, session: Session) => {
            const userId = messageData.user.id
            await checkUser(ctx, userId, messageData)
            const userInfoFormat = await getUserInfoFormat(ctx, userId)
            const img = await render(ctx, userInfoFormat)
            session.sendQueued(img)
        },
    },
    {
        name: '收获阳光',
        alias: ['收', 'sun'], // 别名
        fn: async (messageData: any, ctx: Context, session: Session) => {
            const userId = messageData.user.id
            await checkUser(ctx, userId, messageData)
            const res = await harvestSun(ctx, userId)
            if (res.harvestSunNumber) {
                let text = `把掉落的阳光收集起来了!\n当前阳光池：${res.sunTotal}`
                let url = getImgUrl(`sun.gif`)
                let result = `
                    <message>
                    <image src="${url}"/>
                    </message>
                    <message>${text}</message>
                `
                return await session.sendQueued(result)
            }
            else {
                let url = getImgUrl(`notSun.jpg`)
                let text = '还没有可收集的阳光，再等等吧~'
                let res = `
                    <message>
                        <img width="100" height="100" src="${url}"/>
                    </message>
                    <message>${text}</message>
                `
                return await session.sendQueued(res)
            }
        },
    },
    {
        name: '用户植物',
        alias: ['plt', 'plant', '植物'], // 别名
        fn: async (messageData: any, ctx: Context, session: Session) => {
            const userId = messageData.user.id
            const user = await checkUser(ctx, userId, messageData)
            if (user) {
                console.log('user.plant',user.plant)
            }
        }
    }
]

export default commandList